<?php

/**
 * The Kickstart object is designed to run at the beginning of every request
 * to the application, parsing the urls passed to the application and calling
 * the appropriate controller, template and parameters for this request.
 * 
 * TODO: This is probably a good candidate for an autoloader.
 * 
 * @author Rick Hutcheson
 * @version 0.1
 */
 
 class Kickstart {
    /**
     * @var array $options
     * The options we will use to call the controller for the application.
     * These are parsed from the URL passed to the application.
     */
    private $options = array();
     
    /**
     * Creates a new Kickstart object, initialized with the url passed to the 
     * application.
     * 
     * @param type $URL
     *  The URL Kickstart uses to select the controller, view, and parameters of 
     *  the application.
     */
    public function __construct($URL = "") {
        $this->parseURL($URL);
    }
        
    /**
     * 
     * TODO: Explain.
     * @param unknown_type $url
     * 
     */
	private function parseURL($url) {             
		$urlArray = array();
		
    	if(strlen($url) > 0) {
        	$urlString = filter_var($url, FILTER_SANITIZE_URL);
        	$urlArray  = explode("/", $urlString);
        	
        	// remove empty element from beginning of array
        	// (there will always be a '/' at the beginning of the request.)
        	array_shift($urlArray);
        }
                
		if (!empty($urlArray)) {
			// naming convention
			$controller = ucwords($urlArray[0]) . 'Controller';
			$controllerFile = CONTROLLER_DIR . $controller . '.php';

			// if this file exists,we assume the user wants this controller.                
			if (file_exists($controllerFile)) {
				$this->controllerName = $urlArray[0];
				$this->options['controller'] = $controller;

				// controller exists, so we remove it from the passed options.
				array_shift($urlArray);
			} else {                   
				$this->options['controller'] = 	ucwords(DEFAULT_CONTROLLER). 
				'Controller';   
			}

			// Whether there are parameters or not, we must assign them.
			$this->options['parameters'] = $urlArray;
	
    	} else {
    		// the url had no options, we must set a lot of defaults now.
    		$this->options['controller'] = 	ucwords(DEFAULT_CONTROLLER). 
    									'Controller';                            
    		$this->options['parameters'] = array();
    	} 
	} 
    
    /**
     * This function parses the URl passed to the application, and loads the 
     * appropriate controller and template for the application. It then runs
     * the controller's run() method, which it is guaranteed to have, as it
     * extends JankController.
     */
    public function kick() {
        $controller =  $this->options['controller'];
        $controllerFile = CONTROLLER_DIR.$this->options['controller'].'.php';
        
        require_once($controllerFile);
        
        // Note that this loads the proper controller, based on a naming
        // convention which must be followed. The controller must be named
        // XController, where X is the name passed to the application
        // in the url. Note that upper / lower case does not affect this. 
        $app = new $controller($this->options['parameters']);
    }
 }